package org.dxy.infrastructure.persistent.db.mybatispuls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dxy.infrastructure.persistent.db.mybatispuls.po.query.WorkshopViewPo;
import org.dxy.infrastructure.persistent.db.mybatispuls.mapper.WorkshopViewMapper;
import org.dxy.infrastructure.persistent.db.mybatispuls.service.WorkshopViewService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车间视图服务实现
 */
@Service
public class WorkshopViewServiceImpl extends ServiceImpl<WorkshopViewMapper, WorkshopViewPo> implements WorkshopViewService {

    @Override
    public long countByFactoryId(Long factoryId) {
        if (factoryId == null) {
            return 0;
        }
        return count(new LambdaQueryWrapper<WorkshopViewPo>()
                .eq(WorkshopViewPo::getFactoryId, factoryId));
    }

    @Override
    public boolean existsByWorkshopCode(String workshopCode) {
        if (workshopCode == null || workshopCode.trim().isEmpty()) {
            return false;
        }
        return lambdaQuery()
                .eq(WorkshopViewPo::getWorkshopCode, workshopCode)
                .exists();
    }

    @Override
    public List<WorkshopViewPo> getByFactoryId(Long factoryId) {
        if (factoryId == null) {
            return List.of();
        }
        return list(new LambdaQueryWrapper<WorkshopViewPo>()
                .eq(WorkshopViewPo::getFactoryId, factoryId)
                .orderBy(true, true, WorkshopViewPo::getCreatedTime));
    }
}